package com.ruoyi.project.ledger.service.impl;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.project.ledger.domain.LedgerFile;
import com.ruoyi.project.ledger.mapper.LedgerFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.ledger.mapper.LedgerForkliftInspectMapper;
import com.ruoyi.project.ledger.domain.LedgerForkliftInspect;
import com.ruoyi.project.ledger.service.ILedgerForkliftInspectService;

/**
 * 叉车检查项目Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-04-28
 */
@Service
public class LedgerForkliftInspectServiceImpl implements ILedgerForkliftInspectService 
{
    @Autowired
    private LedgerForkliftInspectMapper ledgerForkliftInspectMapper;
    @Autowired
    private LedgerFileMapper ledgerFileMapper;

    /**
     * 查询叉车检查项目
     * 
     * @param id 叉车检查项目ID
     * @return 叉车检查项目
     */
    @Override
    public LedgerForkliftInspect selectLedgerForkliftInspectById(Long id)
    {

        LedgerForkliftInspect inspect = ledgerForkliftInspectMapper.selectLedgerForkliftInspectById(id);

        if(inspect != null){
            // 获取图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("10");      // 叉车检查项目
            List<Map<String, Object>> imgList = ledgerFileMapper.selectLedgerImgList(ledgerFile);
            inspect.setImgList(imgList);
        }
        return inspect;
    }

    /**
     * 查询叉车检查项目列表
     * 
     * @param ledgerForkliftInspect 叉车检查项目
     * @return 叉车检查项目
     */
    @Override
    public List<LedgerForkliftInspect> selectLedgerForkliftInspectList(LedgerForkliftInspect ledgerForkliftInspect)
    {
        if (StringUtils.isNotBlank(ledgerForkliftInspect.getCheckMonth())){
            ledgerForkliftInspect.setCheckMonth(ledgerForkliftInspect.getCheckMonth()+"-01");
        }
        return ledgerForkliftInspectMapper.selectLedgerForkliftInspectList(ledgerForkliftInspect);
    }

    /**
     * 新增叉车检查项目
     * 
     * @param ledgerForkliftInspect 叉车检查项目
     * @return 结果
     */
    @Override
    public int insertLedgerForkliftInspect(LedgerForkliftInspect ledgerForkliftInspect)
    {
        ledgerForkliftInspect.setCreateTime(DateUtils.getNowDate());
        ledgerForkliftInspect.setCreateId(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        int i = ledgerForkliftInspectMapper.insertLedgerForkliftInspect(ledgerForkliftInspect);
        // 添加图片
        if(ledgerForkliftInspect.getImgs() != null && ledgerForkliftInspect.getImgs().length > 0){
            for(String path : ledgerForkliftInspect.getImgs()){
                LedgerFile ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("10");    // 叉车检查项目
                ledgerFile.setLedgerId(ledgerForkliftInspect.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerForkliftInspect.getCreateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 修改叉车检查项目
     * 
     * @param ledgerForkliftInspect 叉车检查项目
     * @return 结果
     */
    @Override
    public int updateLedgerForkliftInspect(LedgerForkliftInspect ledgerForkliftInspect)
    {
        ledgerForkliftInspect.setUpdateTime(DateUtils.getNowDate());
        ledgerForkliftInspect.setUpdateId(String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId()));
        int i = ledgerForkliftInspectMapper.updateLedgerForkliftInspect(ledgerForkliftInspect);
        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(ledgerForkliftInspect.getId());
        ledgerFile.setLedgerType("10");      // 叉车检查项目
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        if(ledgerForkliftInspect.getImgs() != null && ledgerForkliftInspect.getImgs().length > 0){
            for(String path : ledgerForkliftInspect.getImgs()){
                ledgerFile = new LedgerFile();
                ledgerFile.setLedgerType("10");    // 叉车检查项目
                ledgerFile.setLedgerId(ledgerForkliftInspect.getId());
                ledgerFile.setFilePath(path);
                ledgerFile.setCreateId(ledgerForkliftInspect.getUpdateId());
                ledgerFile.setCreateTime(DateUtils.getNowDate());
                ledgerFileMapper.insertLedgerFile(ledgerFile);
            }
        }
        return i;
    }

    /**
     * 批量删除叉车检查项目
     * 
     * @param ids 需要删除的叉车检查项目ID
     * @return 结果
     */
    @Override
    public int deleteLedgerForkliftInspectByIds(Long[] ids)
    {
        int i = ledgerForkliftInspectMapper.deleteLedgerForkliftInspectByIds(ids);
        for(Long id : ids){
            // 删除台账图片
            LedgerFile ledgerFile = new LedgerFile();
            ledgerFile.setLedgerId(id);
            ledgerFile.setLedgerType("10");      // 叉车检查项目
            ledgerFileMapper.deleteLedgerImg(ledgerFile);
        }
        return i;
    }

    /**
     * 删除叉车检查项目信息
     * 
     * @param id 叉车检查项目ID
     * @return 结果
     */
    @Override
    public int deleteLedgerForkliftInspectById(Long id)
    {
        int i = ledgerForkliftInspectMapper.deleteLedgerForkliftInspectById(id);
        // 删除台账图片
        LedgerFile ledgerFile = new LedgerFile();
        ledgerFile.setLedgerId(id);
        ledgerFile.setLedgerType("10");      // 叉车检查项目
        ledgerFileMapper.deleteLedgerImg(ledgerFile);
        return i;
    }
}
